//---------------------------------------------------------------------------


#pragma hdrstop

#include "socklib.h"
#include <Mstcpip.h>

//---------------------------------------------------------------------------

#pragma package(smart_init)

int SetSocketRcvTimeout(SOCKET sock,long sec,long usec)
{
 timeval tv;
 tv.tv_sec=sec;
 tv.tv_usec=usec;
 return setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(const char*)&tv,sizeof(timeval));
}

int SetSocketSndTimeout(SOCKET sock,long sec,long usec)
{
 timeval tv;
 tv.tv_sec=sec;
 tv.tv_usec=usec;
 return setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(const char*)&tv,sizeof(timeval));
}

bool SetSocketKeepAlive(SOCKET sock,u_long keepalivetime,u_long keepaliveinterval)
{
 tcp_keepalive alive = {1, keepalivetime, keepaliveinterval};
 DWORD len;
 if(WSAIoctl(sock, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), 0, 0, (DWORD*)&len, 0, 0)==0)
	return 1;
 else
 	return 0;	
}

bool UnsetSocketKeepAlive(SOCKET sock)
{
 BOOL b=false;
 return setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,(const char*)&b,sizeof(BOOL));
}
